package com.oasys.ecxeption;

import com.oasys.enums.OaResponseCode;
import com.oasys.utils.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mis_wu
 * @date 2022/05/12
 * 全局异常处理
 */
@RestControllerAdvice
@SuppressWarnings("all")
public class GlobalExceptionHandler {

    //自定义控制器异常方法,可处理控制器中的所有异常
    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult<Object> exceptionHandler(Exception e, HttpServletResponse resp){
        e.printStackTrace();
        String msg = "系统异常,请联系管理员!!!";
        if (e instanceof BusinessException) {
            msg = e.getMessage();
        }
        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode(500);
        responseResult.setMsg(msg);
        return responseResult;
    }
}
